<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('盘点主单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
<!--                        <li>-->
<!--                            <label class="is-required">仓库：</label>-->
<!--                            <select id="warehouseId" name="warehouseId" th:with="type=${session.warehouse}">-->
<!--                                &lt;!&ndash;								<option   th:value="${session.warehouseId}" th:text="${@dict.getDictLabel('${session.warehouseId}')}"></option>&ndash;&gt;-->
<!--                                <option th:each="dict : ${type}" th:text="${dict.warehouseName}" th:value="${dict.warehouseId}"></option>-->
<!--                            </select>-->
<!--                        </li>-->

                        <li>
                            <label>仓库：</label>
                            <select name="warehouseId" th:with="type=${@dict.getType('is_house')}" required>
                                <!--<option value="">请选择</option>-->
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>

                        <li>
                            <label>单据状态：</label>
                            <select name="status" th:with="type=${@dict.getType('dj_status')}" required>
                                <option value="">请选择</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                            </select>
                        </li>
                        <li>
                            <label>出库单id：</label><input type="text" name="stockoutId"/>
                        </li>
                        <li>
                            <label>出库类型：</label><select name="outType">
                            <option value="6">盘点出库</option>
                        </select>
                        </li>

                        <li>
                            <label>建单时间：</label><input type="text" placeholder="请单击选择开始时间：" name="beginDate" id="startTime"/>---
                            <input type="text" placeholder="请单击选择结束时间：" name="finishDate" id="endTime"/>
                        </li>

                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
<!--                        <li>-->
<!--                            <label>出口：</label>-->
<!--                            <select id="exitId">-->
<!--                                <option value="">请选择</option>-->
<!--                                <option value="1">出口1</option>-->
<!--                                <option value="2">出口2</option>-->
<!--                                <option value="3">出口3</option>-->
<!--                            </select>-->
<!--                        </li>-->
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="updateStatus(2)">
                <i class="fa fa-plus"></i> 单据审核
            </a>
            <a class="btn btn-primary " onclick="updateStatus(4)">
                <i class="fa fa-edit"></i> 单据确认
            </a>
            <a class="btn btn-danger " onclick="updateStatus(5)">
                <i class="fa fa-remove"></i> 单据作废
            </a>
            拣选台
            <select id="jxt">
                <option value="">请选择</option>
                <option value="1">1号拣选台</option>
                <option value="2">2号拣选台</option>
                <option value="3">3号拣选台</option>
                <option value="4">4号拣选台</option>
            </select>
            <a class="btn btn-warning" onclick="autoAllocation()">
                <i class="fa fa-download"></i> 自动分配
            </a>
        </div>
        <div id="div1" style="height:350px" class="col-sm-8 select-table">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
        <div id="div2" style="height:350px" class="col-sm-8 select-table">
            <table id="bootstrap-table2" data-mobile-responsive="true"></table>
        </div>

    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    var prefix = ctx + "out/pandian";
    var outtype = [[${@dict.getType('outtype')}]];
    var djstatus = [[${@dict.getType('dj_status')}]];
    var productStatus = [[${@dict.getType('product_status')}]];
    var warehouse = [[${@dict.getType('is_house')}]];
    var stockinSource = [[${@dict.getType('stockin_source')}]];
    var outtype = [[${@dict.getType('outtype')}]];


    layui.use('laydate', function () {
        var laydates = layui.laydate;
        laydates.render({
            elem: '#startTime'
            , type: 'datetime',
            /* ,range: '到' */
            format: 'yyyy-MM-dd HH:mm:ss'
        })
    })

    layui.use('laydate', function () {
        var laydates = layui.laydate;
        laydates.render({
            elem: '#endTime'
            , type: 'datetime',
            /* ,range: '到' */
            format: 'yyyy-MM-dd HH:mm:ss'
        })
    })

    //子表
    var options2 = {
        search: false,
        showRefresh: true,
        showColumns: true,
        minimumCountColumns: 2,
        clickToSelect: true,//是否启用点击选中行
        height: $("#div2").height() / 1,
        sortName: "stockoutDetailId",
        sortOrder: "asc",
        modalName: "入库单据子表",
        method: 'post',
        singleSelect:true,
        striped: true, //是否显示行间隔色
        pagination: true,//显示分页
        sidePagination: "client",
        pageNumber: 1,
        pageSize: 15,
        pageList: [10, 25],
        columns:
            [{
                checkbox: true
            },
                {
                    field: 'stockoutDetailId',
                    title: '行号'
                },
                {
                    field: 'stockoutGuid',
                    title: '出库单guid',
                    visible: false
                },
                {
                    field: 'stockoutId',
                    title: '出库单号',
                    align: 'center'
                },
                {
                    field: 'productId',
                    title: '物品编号',
                    align: 'center'
                },
                {
                    field: 'kechaiQuantity',
                    title: '物品名称',
                    align: 'center'
                },
                {
                    field: 'lotNo',
                    title: '批号',
                    align: 'center',
                    visible: true
                },

                {
                    field: 'price',
                    title: '物品状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(productStatus, value);
                    }
                },
                {
                    field: 'unitName',
                    title: '生产时间',
                    align: 'center'
                },
                {
                    field: 'entryid',
                    title: '供应商',
                    align: 'center'
                },{
                field: 'outId',
                title: '客户',
                align: 'center'
            },{
                field: 'erpStockoutId',
                title: '暂存单号',
                align: 'center'
            },
                {
                    field: 'quantityX',
                    title: '重量',
                    align: 'center'
                },
                {
                    field: 'quantity',
                    title: '计划数量',
                    align: 'center'
                },
                {
                    field: 'allotQuantity',
                    title: '分配数量',
                    align: 'center'
                },
                {
                    field: 'assignNum',
                    title: '实际数量',
                    align: 'center'
                }
            ],
        onLoadSuccess(data) {
            switch (data.code) {
                case  500:
                    options2.url = "";
                    $('#bootstrap-table2').bootstrapTable("removeAll")
                    $.modal.alertWarning("查询出错！");
                    break;
                default:
                    break;
            }
        },
    }
    $('#bootstrap-table2').bootstrapTable(options2)

    //主表
    var options = {
        url: "/out/outStock/list",
        detailView: false,//父子表
        search: false,
        showRefresh: true,
        showColumns: true,
        singleSelect:true,
        minimumCountColumns: 2,
        clickToSelect: true,//是否启用点击选中行
        height: $("#div1").height() / 1,
        updateUrl: prefix + "/edit/{id}",
        modalName: "出库主单",
        sortName: "createDate",
        sortOrder: "desc",
        columns: [{
            checkbox: true
        },
            {
                field: 'stockoutGuid',
                title: '出库单guid',
                align: 'center',
                visible: false
            },
            {
                field: 'stockoutId',
                title: '出库单号',
                align: 'center'
            },
            {
                field: 'outType',
                title: '单据类型',
                align: 'center',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(outtype, value);
                }

            },
            {
                field: 'createManId',
                title: '建单人',
                align: 'center'
            },
            {
                field: 'status',
                title: '单据状态',
                align: 'center',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(djstatus, value);
                }
            },
            {
                field: 'classNowId',
                title: '客户',
                align: 'center',
                visible: false
            },
            {
                field: 'stockoutSource',
                title: '订单来源',
                align: 'center'
            },
            {
                field: 'createDate',
                title: '建单时间',
                align: 'center',

            }],

        //双击显示子表
        onClickRow: function (t, e) {
            options2.url = ctx + "out/outStockDetail/list?stockoutId=" + t.stockoutId + "&status=" + t.status;
            $('#bootstrap-table2').bootstrapTable("refresh", options2)
        }
    };
    $.table.init(options);


    //单据审核，单据确认，单据审核
    function updateStatus(newStatusValue) {

        var row = $("#bootstrap-table").bootstrapTable('getSelections');
        if (row.length == 0) {
            $.modal.alertWarning("请至少选择一条数据")
        }
        var stockoutIds = [];
        for (var i = 0; i < row.length; i++) {
            stockoutIds.push(row[i].stockoutId);
        }
        var idParam = {};
        idParam.stockoutIds = stockoutIds;
        idParam.status = row[0].status;
        idParam.newStatus = newStatusValue;

        $.ajax({
            "url": "/out/outStock/updateStatus",
            "type": "POST",
            "dataType": "json",
            "data": idParam,
            "success": function (result) {
                $.modal.alertWarning(result.errorInfo);
                $.table.search();
            }
        });
    }

    //自动分配
    function autoAllocation() {
        var row = $("#bootstrap-table").bootstrapTable('getSelections');
        if (row.length == 0) {
            $.modal.alertWarning("请至少选择一条数据")
            return;
        }

        var jxt=$("#jxt").val();
        if(jxt==''||jxt==null){
            $.modal.alertError("请选择拣选台！")
            return;
        }

        var stockoutIds = [];
        for (var i = 0; i < row.length; i++) {
            if(row[i].status!=2&&row[i].status!=3){
                $.modal.alertWarning("单据未审核！")
                return;
            }
            stockoutIds.push(row[i].stockoutId)
        }
        var outType = row[0].outType;
        var params = {};
        params.stockoutIds = stockoutIds;
        params.outType = outType;
        params.exitId=jxt;

        $.ajax({
            "url": "/pandian/autoAllocation",
            "type": "POST",
            "dataType": "json",
            "data": params,
            "success": function (result) {
                $.modal.alertWarning(result.errorInfo);
                $.table.search();
            }
        });
    }

</script>
</body>
</html>